
package com.shangyueshang.controller.basic;

import com.shangyueshang.model.Shop;
import com.shangyueshang.request.IdDTO;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.request.ShopDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.service.ShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/14 11:31
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
	
	@Resource
	private ShopService shopService;
	
	/**
	 * 获取所有店铺信息
	 *
	 * @return
	 */
	@GetMapping("/findAll")
	@PreAuthorize("hasAuthority('basic:shop:view')")
	public HttpResult findAll() {
		List<Shop> list = shopService.list();
		list.sort(Comparator.comparing(Shop::getCreateTime).reversed());
		return HttpResult.ok(list);
	}
	
	/**
	 * 保存店铺信息
	 *
	 * @param shopDTO
	 * @return
	 */
	@PostMapping("/save")
	@PreAuthorize("hasAuthority('basic:shop:add') and hasAuthority('basic:shop:edit')")
	public HttpResult save(@Valid @RequestBody ShopDTO shopDTO) {
		return shopService.save(shopDTO);
	}
	
	/**
	 * 删除店铺集合
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('basic:shop:delete')")
	public HttpResult delete(@NotEmpty @RequestBody List<IdDTO> ids) {
		shopService.remove(ids.stream().map(IdDTO::getId).collect(Collectors.toList()));
		return HttpResult.ok();
	}
	
	/**
	 * 分页查询
	 *
	 * @param pageReq
	 * @return
	 */
	@PostMapping("/findPage")
	@PreAuthorize("hasAuthority('basic:shop:view')")
	public HttpResult findPage(@RequestBody PageReq pageReq) {
		return HttpResult.ok(shopService.findPage(pageReq));
	}
	
}
